/*
* PulverizingService.java
* Created on  202015/7/20 13:49
* 版本       修改时间          作者      修改内容
* V1.0.1    202015/7/20       panzhuowen    初始版本
*
*/

package com.infitecs.eyas.mill.service;

import com.infitecs.eyas.mill.dao.PulverizingDao;
import com.infitecs.eyas.mill.model.PulverizingBase;
import com.infitecs.eyas.mill.model.PulverizingSection;
import com.infitecs.eyas.mill.model.PulverizingTao;
import com.infitecs.eyas.session.SessionService;
import com.infitecs.eyas.session.model.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类的描述信息
 *
 * @author panzhuowen
 * @version 1.0.1
 */
@Service
public class PulverizingService {

    @Autowired
    private SessionService sessionService;

    @Autowired
    private PulverizingDao pulverizingDao;

    public int addPulverizing() {
        int caseId = sessionService.getCaseId();
        PulverizingBase pulverizingBase = new PulverizingBase();
        pulverizingBase.setCaseId(caseId);
        pulverizingBase.setTitle("道体阻力1");
        pulverizingDao.addPulverizingBase(pulverizingBase);
        PulverizingTao pulverizingTao = new PulverizingTao();
        pulverizingTao.setBaseId(pulverizingBase.getId());
        pulverizingTao.setLevel(1);
        pulverizingTao.setTitle("区间1");
        pulverizingDao.addPulverizingTao(pulverizingTao);
        PulverizingSection pulverizingSection = new PulverizingSection();
        pulverizingSection.setTaoId(pulverizingTao.getId());
        pulverizingSection.setSectionTitle("区间1");
        for (int i = 1; i <= 4; i++) {
            switch (i) {
                case 1:
                    pulverizingSection.setChildTitle("管段1");
                    pulverizingSection.setSectionType(i);
                    break;
                case 2:
                    pulverizingSection.setChildTitle("零件1");
                    pulverizingSection.setSectionType(i);
                    break;
                case 3:
                    pulverizingSection.setChildTitle("部件1");
                    pulverizingSection.setSectionType(i);
                    break;
                case 4:
                    pulverizingSection.setChildTitle("设备1");
                    pulverizingSection.setSectionType(i);
                    break;
            }
            pulverizingSection.setClassA("btn green");
            pulverizingSection.setClassI("fa fa-plus");
            pulverizingDao.addPulverizingSection(pulverizingSection);
        }
        insertOther(pulverizingTao.getId());
        return pulverizingBase.getId();
    }

    public List<PulverizingBase> getRecover() {
        int caseId = sessionService.getCaseId();
        List<PulverizingBase> baseList = pulverizingDao.getPulverizingBaseListByCaseId(caseId);
        for (PulverizingBase base : baseList) {
            List<PulverizingTao> taoList = pulverizingDao.getPulverizingTaoListByBaseId(base.getId());
            base.setTaoList(taoList);
            for (PulverizingTao tao : taoList) {
                List<PulverizingSection> sectionList = pulverizingDao.getPulverizingListByTaoId(tao.getId());
                tao.setSectionList(sectionList);
            }
        }
        return baseList;
    }

    public boolean checkDBIsNotEmpty() {
        int caseId = sessionService.getCaseId();
        List<PulverizingBase> baseList = pulverizingDao.getPulverizingBaseListByCaseId(caseId);
        if (!baseList.isEmpty()) {
            List<PulverizingTao> taoList = pulverizingDao.getPulverizingTaoListByBaseId(baseList.get(0).getId());
            if (!taoList.isEmpty()) {
                List<PulverizingSection> sectionList = pulverizingDao.getPulverizingListByTaoId(taoList.get(0).getId());
                if (!sectionList.isEmpty()) {
                    return true;
                }
            }
        }
        return false;
    }

    public int addSection(PulverizingTao pulverizingTao) {
        pulverizingDao.addPulverizingTao(pulverizingTao);
        PulverizingSection pulverizingSection = new PulverizingSection();
        pulverizingSection.setTaoId(pulverizingTao.getId());
        pulverizingSection.setSectionTitle(pulverizingTao.getTitle());
        for (int i = 1; i <= 4; i++) {
            switch (i) {
                case 1:
                    pulverizingSection.setChildTitle("管段1");
                    pulverizingSection.setSectionType(i);
                    break;
                case 2:
                    pulverizingSection.setChildTitle("零件1");
                    pulverizingSection.setSectionType(i);
                    break;
                case 3:
                    pulverizingSection.setChildTitle("部件1");
                    pulverizingSection.setSectionType(i);
                    break;
                case 4:
                    pulverizingSection.setChildTitle("设备1");
                    pulverizingSection.setSectionType(i);
                    break;
            }
            pulverizingSection.setClassA("btn green");
            pulverizingSection.setClassI("fa fa-plus");
            pulverizingDao.addPulverizingSection(pulverizingSection);
        }
        insertOther(pulverizingTao.getId());
        return pulverizingTao.getId();
    }

    public int addSec(PulverizingSection pulverizingSection) {
        pulverizingSection.setSectionTitle("xxx");
        pulverizingDao.addPulverizingSection(pulverizingSection);
        return pulverizingSection.getId();
    }

    private void insertOther(int taoId) {
        PulverizingSection pulverizingSection = new PulverizingSection();
        pulverizingSection.setTaoId(taoId);
        for (int i = 0;i < 3; i++) {
            switch (i) {
                case 0:
                    pulverizingSection.setChildTitle("提升压头损失");
                    break;
                case 1:
                    pulverizingSection.setChildTitle("加速损失");
                    break;
                case 2:
                    pulverizingSection.setChildTitle("自生通风");
                    break;
            }
            pulverizingSection.setSectionType(5);
            pulverizingSection.setClassA("btn green");
            pulverizingSection.setClassI("fa fa-plus");
            pulverizingDao.addPulverizingSection(pulverizingSection);
        }
    }

    public void delSec(int id) {
        pulverizingDao.delSec(id);
    }

    public void updateBaseTitle(PulverizingBase pulverizingBase) {
        pulverizingDao.updateBaseTitle(pulverizingBase);
    }

    public void updateTaoTitle(PulverizingTao pulverizingTao) {
        pulverizingDao.updateTaoTitle(pulverizingTao);
    }

    public void updateChildTitle(PulverizingSection pulverizingSection) {
        pulverizingDao.updateChildTitle(pulverizingSection);
    }

    //计算和变量值更新部分
    public void updateBaseParam(PulverizingBase pulverizingBase){
        pulverizingDao.updateBaseParam(pulverizingBase);
    }

    public void updateSection(PulverizingSection pulverizingSection){
        pulverizingDao.updateSection(pulverizingSection);
    }
}
